#include "ClientSocket.h"
#include <WinSock2.h>

/* Destructor */
ClientSocket::~ClientSocket()
{
	// Destroy some stuff.
}

/*-------------------------------------------------------------------------------------
Call this before any other functions.
Initializes the client and connects to the server supplied as a command line argument.
Returns 'false' if there was an error and 'true' if otherwise.
---------------------------------------------------------------------------------------*/
bool ClientSocket::initialize()
{
	hostent *host_info = gethostbyname(m_host); // Retreive host info from host name
	if (!host_info)
	{
		std::cerr << "ClientSocket::initialize() gethostbyname error" << std::endl;
	}

	in_addr *address = (in_addr*)host_info->h_addr_list[0];
	memset(m_host, NULL, strlen(m_host));
	strcpy(m_host, inet_ntoa(*address)); // strcpy may be unsafe, so strcpy_s would probably be smarter.
	//strcpy_s(m_host, strlen(m_host), inet_ntoa(*address));

	if ((m_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
	{
		std::cerr << "ClientSocket::initialize - socket() error: " << WSAGetLastError() << std::endl;
		return false;
	}

	memset(&serveraddr, 0, sizeof(serveraddr));
    serveraddr.sin_family      = AF_INET;
    serveraddr.sin_addr.s_addr = inet_addr(m_host);
    serveraddr.sin_port        = htons((unsigned short) m_port);

	if (connect(m_sock, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0)
	{
		std::cerr << "ClientSocket::initialize - connect() error: " << WSAGetLastError() << std::endl;
		return false;
	}

	std::cout << "Connected successfully to " << host_info->h_name << " (" <<  m_host << ")" << std::endl;

	return true;
}

/*----------------------------------------------------------------------------
Should be called after the QUIT command has been issued in order to close the 
connection.
------------------------------------------------------------------------------*/
void ClientSocket::closeConnection()
{
	if (m_sock)
	{
		closesocket(m_sock);
	}
}

/*------------------------------------------------------
Sends a string containing an IRC command to the server.
--------------------------------------------------------*/
void ClientSocket::sendData(std::string commandString)
{
	commandString += "\r\n";
	if (send(m_sock, commandString.c_str(), commandString.length(), 0) != commandString.length())
	{
        std::cerr << "ClientSocket::sendData - send() sent a different number of bytes than expected" << std::endl;
	}
}

/*-------------------------------------------------------------------
Receives a string of data from the server and returns it as a string.
---------------------------------------------------------------------*/
std::string ClientSocket::receiveData()
{
	m_response = "";
	int recvLength = recv(m_sock, (char*)&m_buffer, BUFFERSIZE, 0);
	if (recvLength > 0)
	{
		m_response = std::string(m_buffer).substr(0, recvLength);
	}
	return m_response;
}

/*-------------------------
Gets an error or something.
---------------------------*/
void ClientSocket::getErrors()
{

}